package com.kla.power.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.kla.power.entity.AlarmEntity;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 报警/故障 Mapper 接口
 * </p>
 *
 * @author WuZH
 * @since 2023-06-23
 */
@Mapper
public interface AlarmMapper extends BaseMapper<AlarmEntity> {


    @Insert ( value = "INSERT INTO alarm_info(id,alarmCode,alarmTitle,type,deviceSn,stationId,alarmUser,createTime) VALUE(#{alarmEntity.id},#{alarmEntity.alarmCode},#{alarmEntity.alarmTitle},#{alarmEntity.type},#{alarmEntity.deviceSn},#{alarmEntity.stationId},#{alarmEntity.alarmUser},#{alarmEntity.createTime})" )
    Integer saveSingle ( @Param ( "alarmEntity" ) AlarmEntity alarmEntity );


    @Select ( value = "<script>SELECT * FROM alarm_info ai\n" +
            "         LEFT JOIN device_info di ON ai.deviceSn = di.deviceSn\n" +
            "         LEFT JOIN station_info si ON di.stationId = si.id\n" +
            "<trim prefix=\"WHERE\" prefixOverrides=\"and | or\">" +
            "<if test='alarmEntity.merchantId!=null'> AND si.merchantId=#{alarmEntity.merchantId} </if>" +
            "<if test='alarmEntity.deviceSn!=null'> AND ai.deviceSn=#{alarmEntity.deviceSn} </if>" +
            "<if test='alarmEntity.type!=null'> AND ai.type=#{alarmEntity.type} </if>" +
            "<if test='alarmEntity.status!=null'> AND ai.status=#{alarmEntity.status} </if>" +
            "<if test='alarmEntity.stationName!=null'> AND si.stationName LIKE CONCAT('%', #{alarmEntity.stationName, jdbcType=VARCHAR}, '%') </if>" +
            "ORDER BY ai.createTime DESC LIMIT #{alarmEntity.page}, #{alarmEntity.limit}</trim></script>" )
    List<AlarmEntity> queryList ( @Param ( "alarmEntity" ) AlarmEntity alarmEntity );


    @Select ( value = "<script>SELECT COUNT(1) FROM alarm_info ai\n" +
            "         LEFT JOIN device_info di ON ai.deviceSn = di.deviceSn\n" +
            "         LEFT JOIN station_info si ON di.stationId = si.id\n" +
            "<trim prefix=\"WHERE\" prefixOverrides=\"and | or\">" +
            "<if test='alarmEntity.merchantId!=null'> AND si.merchantId=#{alarmEntity.merchantId} </if>" +
            "<if test='alarmEntity.deviceSn!=null'> AND ai.deviceSn=#{alarmEntity.deviceSn} </if>" +
            "<if test='alarmEntity.type!=null'> AND ai.type=#{alarmEntity.type} </if>" +
            "<if test='alarmEntity.status!=null'> AND ai.status=#{alarmEntity.status} </if>" +
            "<if test='alarmEntity.stationName!=null'> AND si.stationName LIKE CONCAT('%', #{alarmEntity.stationName, jdbcType=VARCHAR}, '%') </if>" +
            "</trim></script>" )
    Integer queryCount( @Param ( "alarmEntity" ) AlarmEntity alarmEntity );



    @Select ( value = "<script>SELECT *,si.stationName AS stationName FROM alarm_info ai LEFT JOIN station_info si ON ai.stationId=si.id WHERE ai.id=#{alarmEntity.id}</script>" )
    AlarmEntity querySingle ( @Param ( "alarmEntity" ) AlarmEntity alarmEntity );

    /**
     * 查询一个商户下机器人的所有异常信息
     *
     * @param alarmEntity
     * @return
     */
    @Select ( value = "SELECT * FROM alarm_info ai LEFT JOIN robot_info ri ON ai.deviceSn=ri.robotSn LEFT JOIN station_info si ON si.id=ri.stationId WHERE ai.type=#{alarmEntity.type} AND date_format(ai.createTime, '%Y-%m-%d')=date_format(#{alarmEntity.createTime}, '%Y-%m-%d') AND si.merchantId=#{alarmEntity.merchantId}" )
    List<AlarmEntity> queryListByMerchantRobot ( @Param ( "alarmEntity" ) AlarmEntity alarmEntity );



    /**
     * 查询商户下出现告警电站，按电站分组，给大屏统计使用
     * @param alarmEntity
     * @return
     */
    @Select (value = "SELECT * from alarm_info ai\n" +
            "         LEFT JOIN device_info di ON ai.deviceSn = di.deviceSn\n" +
            "         LEFT JOIN station_info si ON di.stationId = si.id\n" +
            "WHERE si.merchantId=#{alarmEntity.merchantId}\n" +
            "GROUP BY si.id")
    List<AlarmEntity> queryScreeAlarm (@Param ("alarmEntity") AlarmEntity alarmEntity);

    /**
     * 查询商户下出现告警电站，按电站分组，给大屏统计使用
     * @param alarmEntity
     * @return
     */
    @Select (value = "SELECT * from alarm_info ai\n" +
            "         LEFT JOIN device_info di ON ai.deviceSn = di.deviceSn\n" +
            "         LEFT JOIN station_info si ON di.stationId = si.id\n" +
            "WHERE si.id=#{alarmEntity.stationId} AND status=1")
    List<AlarmEntity> queryAlarmByStation (@Param ("alarmEntity") AlarmEntity alarmEntity);

    /**
     * 查询一个逆变器今天是否已插入警告信息
     * @param alarmEntity
     * @return
     */
    @Select ( value = "<script>select * from alarm_info ai " +
            "<trim prefix=\"WHERE\" prefixOverrides=\"and | or\">" +
            "<if test='alarmEntity.deviceSn!=null'> AND ai.deviceSn=#{alarmEntity.deviceSn} </if>" +
            "<if test='alarmEntity.status!=null'> AND ai.status=#{alarmEntity.status} </if>" +
            "<if test='alarmEntity.alarmCode!=null'> AND ai.alarmCode=#{alarmEntity.alarmCode} </if>" +
            " and date_format(createTime, '%Y-%m-%d')=date_format(current_date(), '%Y-%m-%d')" +
            "</trim></script>")
    List<AlarmEntity> queryTodayList(@Param ("alarmEntity") AlarmEntity alarmEntity);


    /**
     * 一次性求出所有电站的警告数量
     * @param ids
     * @return
     */
    @Select (value = "SELECT si.id AS stationId from alarm_info ai\n" +
            "         LEFT JOIN device_info di ON ai.deviceSn = di.deviceSn\n" +
            "         LEFT JOIN station_info si ON di.stationId = si.id\n" +
            "WHERE si.id IN (#{ids}) AND status=1")
    List<AlarmEntity> queryAlarmCountByStation (List<String> ids);

}
